package org.dromara.bus.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.bus.domain.bo.YxdStashBo;
import org.dromara.bus.domain.vo.YxdStashVo;
import org.dromara.bus.domain.YxdStash;
import org.dromara.bus.mapper.YxdStashMapper;
import org.dromara.bus.service.IYxdStashService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 熠星达仓库Service业务层处理
 *
 * @author csy
 * @date 2024-03-19
 */
@RequiredArgsConstructor
@Service
public class YxdStashServiceImpl implements IYxdStashService {

    private final YxdStashMapper baseMapper;

    /**
     * 查询熠星达仓库
     */
    @Override
    public YxdStashVo queryById(Long stashId){
        return baseMapper.selectVoById(stashId);
    }

    /**
     * 查询熠星达仓库列表
     */
    @Override
    public TableDataInfo<YxdStashVo> queryPageList(YxdStashBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<YxdStash> lqw = buildQueryWrapper(bo);
        Page<YxdStashVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询熠星达仓库列表
     */
    @Override
    public List<YxdStashVo> queryList(YxdStashBo bo) {
        LambdaQueryWrapper<YxdStash> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<YxdStash> buildQueryWrapper(YxdStashBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<YxdStash> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getQrCode()), YxdStash::getQrCode, bo.getQrCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), YxdStash::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getPosition()), YxdStash::getPosition, bo.getPosition());
        lqw.eq(StringUtils.isNotBlank(bo.getAddr()), YxdStash::getAddr, bo.getAddr());
        lqw.eq(bo.getLongitude() != null, YxdStash::getLongitude, bo.getLongitude());
        lqw.eq(bo.getLatitude() != null, YxdStash::getLatitude, bo.getLatitude());
        lqw.eq(StringUtils.isNotBlank(bo.getContact()), YxdStash::getContact, bo.getContact());
        lqw.eq(StringUtils.isNotBlank(bo.getTelephone()), YxdStash::getTelephone, bo.getTelephone());
        lqw.eq(bo.getOpenTime() != null, YxdStash::getOpenTime, bo.getOpenTime());
        lqw.eq(bo.getCloseTime() != null, YxdStash::getCloseTime, bo.getCloseTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), YxdStash::getStatus, bo.getStatus());
        lqw.eq(bo.getDeptId() != null, YxdStash::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptCode()), YxdStash::getDeptCode, bo.getDeptCode());
        return lqw;
    }

    /**
     * 新增熠星达仓库
     */
    @Override
    public Boolean insertByBo(YxdStashBo bo) {
        YxdStash add = MapstructUtils.convert(bo, YxdStash.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setStashId(add.getStashId());
        }
        return flag;
    }

    /**
     * 修改熠星达仓库
     */
    @Override
    public Boolean updateByBo(YxdStashBo bo) {
        YxdStash update = MapstructUtils.convert(bo, YxdStash.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(YxdStash entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除熠星达仓库
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
